package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.ReturnListDao;
import com.atguigu.jxc.dao.ReturnListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.GoodsCount;
import com.atguigu.jxc.entity.ReturnList;
import com.atguigu.jxc.entity.ReturnListGoods;
import com.atguigu.jxc.service.ReturnListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class ReturnListGoodsServiceImpl implements ReturnListGoodsService {
    @Autowired
    private ReturnListDao returnListDao;
    @Autowired
    private ReturnListGoodsDao returnListGoodsDao;
    @Autowired
    private GoodsDao goodsDao;
    private Gson gson = new Gson();

    @Override
    @Transactional
    public ServiceVO save(ReturnList returnList, String returnListGoodsStr) {
        if (StringUtil.isEmpty(returnListGoodsStr)) {
            return new ServiceVO(400, "退货的商品不能为空");
        }
        // 获得单号
        Integer returnListId = returnList.getReturnListId();
        List<ReturnListGoods> listGoods = gson.fromJson(returnListGoodsStr, new TypeToken<List<ReturnListGoods>>() {
        }.getType());
        for (ReturnListGoods listGood : listGoods) {
            //添加退货单号
            listGood.setReturnListId(returnListId);
            //获得原库存
            Goods good = goodsDao.getById(listGood.getGoodsId());
            Integer inventoryQuantity = good.getInventoryQuantity();
            if (inventoryQuantity < listGood.getGoodsNum()) {
                return new ServiceVO(400, "退货的数量不能大于库存");
            }
            //添加出库商品
            returnListGoodsDao.save(listGood);
            //修改商品库存
            goodsDao.updateQuantity(listGood.getGoodsId(), inventoryQuantity - listGood.getGoodsNum());
        }
        returnListDao.save(returnList);
        return new ServiceVO(100, "请求成功");
    }

    @Override
    public List<ReturnList> list(String returnNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try {
            start = DateUtil.StringToDate(sTime, format);
            end = DateUtil.StringToDate(eTime, format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return returnListDao.list(returnNumber, supplierId, state, start, end);
    }

    @Override
    public void delete(Integer returnListId) {
        List<ReturnListGoods> list = returnListGoodsDao.getByListId(returnListId);
        if (null != list && list.size() > 0) {
            returnListGoodsDao.deleteByListId(returnListId);
        }
        returnListDao.deleteById(returnListId);
    }

    @Override
    public List<GoodsCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }
       return returnListGoodsDao.selectCount(start,end,goodsTypeId,codeOrName);
    }

}
